import request from "./request.js"

/**
 * KTV命令发送工具类
 */
class KTVCommandUtils {
	constructor() {
		this.isLoading = false;
	}

	/**
	 * 发送KTV控制命令
	 */
	async sendCommand(orderId, type, info = '') {
		if (this.isLoading) {
			console.log('正在执行其他操作，请稍后...');
			return false;
		}
		type = encodeURIComponent(encodeURIComponent(type));
		this.isLoading = true;
		try {
			const res = await request.httpTokenRequest({
				url: '/api/ktv/pushCommand?order_id=' + orderId + '&type=' + type + '&info=' + info,
				method: 'get',
			});

			if (res.data.code === 1) {
				console.log(`命令执行成功: ${type} ${info}`);
				return true;
			} else {
				throw new Error(res.data.msg || '命令执行失败');
			}
		} catch (error) {
			console.error('发送命令失败:', error);
			uni.showToast({
				title: error.message || '操作失败',
				icon: 'none'
			});
			return false;
		} finally {
			this.isLoading = false;
		}
	}

	/**
	 * 点歌
	 */
	async addSong(orderId, songId) {
		return await this.sendCommand(orderId, 'ADD', songId);
	}

	/**
	 * 获取已点歌单
	 */
	async getVodList(orderId) {
		return await this.sendCommand(orderId, 'SEND_GET_VODLIST');
	}

	/**
	 * 删除已点歌曲
	 */
	async deleteSong(orderId, songId) {
		return await this.sendCommand(orderId, 'DELETE', songId);
	}

	/**
	 * 置顶歌曲
	 */
	async insertSong(orderId, songId) {
		return await this.sendCommand(orderId, 'INSERT', songId);
	}

	/**
	 * 切歌
	 */
	async nextSong(orderId) {
		return await this.sendCommand(orderId, 'NEXT_SONG');
	}

	/**
	 * 重唱
	 */
	async replay(orderId) {
		return await this.sendCommand(orderId, 'REPLAY');
	}

	/**
	 * 播放/暂停
	 */
	async playPause(orderId) {
		return await this.sendCommand(orderId, 'PLAY_PAUSE');
	}

	/**
	 * 呼叫服务
	 */
	async callService(orderId) {
		return await this.sendCommand(orderId, 'SERVER_CONTROL');
	}

	/**
	 * 麦克风音量调节
	 */
	async adjustMic(orderId, increase = true) {
		const type = increase ? 'MIC+' : 'MIC-';
		return await this.sendCommand(orderId, type);
	}

	/**
	 * 音量调节
	 */
	async adjustVolume(orderId, increase = true) {
		const type = increase ? 'VOL+' : 'VOL-';
		return await this.sendCommand(orderId, type);
	}

	/**
	 * 原伴唱切换
	 */
	async toggleVocalBackground(orderId) {
		return await this.sendCommand(orderId, 'YC_BC');
	}

	/**
	 * 灯光控制
	 */
	async controlLight(orderId, mode) {
		// mode: RH(柔和), DG(动感), ML(明亮), RQ(柔情), OFF(全关), ON(全开), 
		// SW(商务), XX(炫酷), YG(摇滚), LM(浪漫), QJ(清冽), AUTO_ON(自动开), AUTO_OFF(自动关)
		return await this.sendCommand(orderId, 'LIGHT_CONTROL', mode);
	}

	/**
	 * 音效控制
	 */
	async controlSoundEffect(orderId, effect) {
		// effect: PRO(专业), POP(流行), SOFT(抒情), ROCK(摇滚), MEET(会议), JZ(爵士)
		return await this.sendCommand(orderId, 'YX_CONTROL', effect);
	}

	/**
	 * 评分开关
	 */
	async toggleScore(orderId, open = true) {
		const info = open ? 'OPEN' : 'CLOSE';
		return await this.sendCommand(orderId, 'score', info);
	}

	/**
	 * 关机
	 */
	async shutdown(orderId) {
		return await this.sendCommand(orderId, 'SHUTDOWN');
	}

	/**
	 * 静音
	 */
	async mute(orderId) {
		return await this.sendCommand(orderId, 'MUTE');
	}

	/**
	 * 取消静音
	 */
	async unmute(orderId) {
		return await this.sendCommand(orderId, 'UNMUTE');
	}

	/**
	 * 批量操作
	 */
	async batchCommands(orderId, commands) {
		const results = [];
		for (const command of commands) {
			const result = await this.sendCommand(orderId, command.type, command.info);
			results.push({
				command,
				success: result
			});

			// 批量操作间隔
			await new Promise(resolve => setTimeout(resolve, 200));
		}
		return results;
	}

	/**
	 * 预设场景
	 */
	async applyScene(orderId, sceneName) {
		const scenes = {
			// 浪漫场景
			romantic: [{
					type: 'LIGHT_CONTROL',
					info: 'LM'
				},
				{
					type: 'YX_CONTROL',
					info: 'SOFT'
				},
				{
					type: 'VOL-',
					info: ''
				}
			],
			// 摇滚场景
			rock: [{
					type: 'LIGHT_CONTROL',
					info: 'YG'
				},
				{
					type: 'YX_CONTROL',
					info: 'ROCK'
				},
				{
					type: 'VOL+',
					info: ''
				}
			],
			// 商务场景
			business: [{
					type: 'LIGHT_CONTROL',
					info: 'SW'
				},
				{
					type: 'YX_CONTROL',
					info: 'MEET'
				},
				{
					type: 'score',
					info: 'CLOSE'
				}
			],
			// 派对场景
			party: [{
					type: 'LIGHT_CONTROL',
					info: 'DG'
				},
				{
					type: 'YX_CONTROL',
					info: 'POP'
				},
				{
					type: 'VOL+',
					info: ''
				}
			]
		};

		const sceneCommands = scenes[sceneName];
		if (!sceneCommands) {
			throw new Error('未知场景');
		}

		return await this.batchCommands(orderId, sceneCommands);
	}
}

// 创建全局实例
const ktvCommand = new KTVCommandUtils();

export default ktvCommand;